﻿/// <reference path="jquery-1.7.1-vsdoc.js" />

$(document).ready(function () {
    ManageDateAndTimes();
    ManageEventOccurrence();

    PrepareDateAndTimes();
    PrepareEventOccurrence();
});

function PrepareEventOccurrence() {
    $("#EventOccurrence").change(function () {
        ManageEventOccurrence();
    });

    $("#DayInstance").change(function () {
        ManageEventOccurrence();
    });
}

function PrepareDateAndTimes() {
    $.datepicker.regional[''] = {
        closeText: 'Done', // Display text for close link
        prevText: 'Prev', // Display text for previous month link
        nextText: 'Next', // Display text for next month link
        currentText: 'Today', // Display text for current month link
        monthNames: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'], // Names of months for drop-down and formatting
        monthNamesShort: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'], // For formatting
        dayNames: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'], // For formatting
        dayNamesShort: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'], // For formatting
        dayNamesMin: ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'], // Column headings for days starting at Sunday
        weekHeader: 'Wk', // Column header for week of the year
        dateFormat: 'm/d/yy', // See format options on parseDate
        firstDay: 1, // The first day of the week, Sun = 0, Mon = 1, ...
        isRTL: false, // True if right-to-left language, false if left-to-right
        showMonthAfterYear: false, // True if the year select precedes month, false for month then year
        yearSuffix: '' // Additional text to append to the year in the month headers
    };

    var dates = $("#StartDate, #EndDate").datepicker({
        defaultDate: "+0d",
        changeMonth: true,
        numberOfMonths: 3,
        onSelect: function (selectedDate) {
            var option = this.id == "StartDate" ? "minDate" : "maxDate",
                    instance = $(this).data("datepicker"),
                    date = $.datepicker.parseDate(
                        instance.settings.dateFormat ||
                        $.datepicker._defaults.dateFormat,
                        selectedDate, instance.settings);
            dates.not(this).datepicker("option", option, date);
            ManageDateAndTimes();
        }
    });

    $("#StartTime").timepickr({ showAnim: "" });
    $("#EndTime").timepickr({ showAnim: "" });

    $("#StartDate").change(function () {
        ManageDateAndTimes();
    });
    $("#EndDate").change(function () {
        ManageDateAndTimes();
    });
    $("#StartTime").change(function () {
        ManageDateAndTimes();
    });
    $("#EndTime").change(function () {
        ManageDateAndTimes();
    });

    $("#IsAllDayEvent").change(function () {
        ManageDateAndTimes();
    });
}

function ManageDateAndTimes() {
    // Manage Disabling of times
    if ($("#EndDate").val() == "" ||
        $("#StartTime").val() == "" ||
        $("#IsAllDayEvent").attr("checked") == "checked") {
        $("#EndTime").attr("disabled", "disabled");
    }
    else {
        $("#EndTime").removeAttr("disabled");
    }

    if ($("#StartDate").val() == "" ||
        $("#IsAllDayEvent").attr("checked") == "checked") {
        $("#StartTime").attr("disabled", "disabled");
    }
    else {
        $("#StartTime").removeAttr("disabled");
    }
    
    // Make sure end time is later than start time
    //  Date picker already controls itself so no need to do anything there
    if ($("#EndTime").val() != "") {
        var startDateTime = new Date(
            $("#StartDate").val() + " " +
            $("#StartTime").val()
        )
        
        var endDateTime = new Date(
            $("#EndDate").val() + " " +
            $("#EndTime").val()
        )

        if (endDateTime < startDateTime) {
            alert("End Time must be greater than Start Time");
        }
    }
}

function ManageEventOccurrence() {
    if ($("#EventOccurrence option:selected").val() == "Single") {
        $("#SingleEventPanel").show();
        $("#RecurrenceEventPanel").hide();
    }
    else {
        $("#SingleEventPanel").hide();
        $("#RecurrenceEventPanel").show();
    }
}
